<template>
  <a-drawer
    :title="title"
    :width="width"
    placement="right"
    :closable="false"
    @close="close"
    :visible="visible">
  
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item label="名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'name', validatorRules.name]" placeholder="请输入名称"></a-input>
        </a-form-item>
        <a-form-item label="first_name" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'firstName', validatorRules.firstName]" placeholder="请输入first_name"></a-input>
        </a-form-item>
        <a-form-item label="价格" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'salePrice', validatorRules.salePrice]" placeholder="请输入价格"></a-input>
        </a-form-item>
        <a-form-item label="category_json" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'categoryJson', validatorRules.categoryJson]" placeholder="请输入category_json"></a-input>
        </a-form-item>
        <a-form-item label="category_ids" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'categoryIds', validatorRules.categoryIds]" placeholder="请输入category_ids"></a-input>
        </a-form-item>
        <a-form-item label="goods_label_ids" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'goodsLabelIds', validatorRules.goodsLabelIds]" placeholder="请输入goods_label_ids"></a-input>
        </a-form-item>
        <a-form-item label="logistics_model_id" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'logisticsModelId', validatorRules.logisticsModelId]" placeholder="请输入logistics_model_id"></a-input>
        </a-form-item>
        <a-form-item label="galley" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'galley', validatorRules.galley]" placeholder="请输入galley"></a-input>
        </a-form-item>
        <a-form-item label="pic_url" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'picUrl', validatorRules.picUrl]" placeholder="请输入pic_url"></a-input>
        </a-form-item>
        <a-form-item label="detail" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'detail', validatorRules.detail]" placeholder="请输入detail"></a-input>
        </a-form-item>
        <a-form-item label="type" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'type', validatorRules.type]" placeholder="请输入type"></a-input>
        </a-form-item>
        <a-form-item label="stock" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'stock', validatorRules.stock]" placeholder="请输入stock"></a-input>
        </a-form-item>
        <a-form-item label="is_pers" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'isPers', validatorRules.isPers]" placeholder="请输入is_pers"></a-input>
        </a-form-item>
        <a-form-item label="is_sell" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'isSell', validatorRules.isSell]" placeholder="请输入is_sell"></a-input>
        </a-form-item>
        <a-form-item label="browser_num" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'browserNum', validatorRules.browserNum]" placeholder="请输入browser_num"></a-input>
        </a-form-item>
        <a-form-item label="sales_num" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'salesNum', validatorRules.salesNum]" placeholder="请输入sales_num"></a-input>
        </a-form-item>
        <a-form-item label="studio_id" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'studioId', validatorRules.studioId]" placeholder="请输入studio_id"></a-input>
        </a-form-item>
        <a-form-item label="now_divide" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'nowDivide', validatorRules.nowDivide]" placeholder="请输入now_divide"></a-input>
        </a-form-item>
        <a-form-item label="divide_into" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'divideInto', validatorRules.divideInto]" placeholder="请输入divide_into"></a-input>
        </a-form-item>
        <a-form-item label="issue_date" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'issueDate', validatorRules.issueDate]" placeholder="请输入issue_date"></a-input>
        </a-form-item>
        <a-form-item label="create_time" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'createTime', validatorRules.createTime]" placeholder="请输入create_time"></a-input>
        </a-form-item>
        <a-form-item label="update_time" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'updateTime', validatorRules.updateTime]" placeholder="请输入update_time"></a-input>
        </a-form-item>
        <a-form-item label="status" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'status', validatorRules.status]" placeholder="请输入status"></a-input>
        </a-form-item>
        <a-form-item label="deleted" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'deleted', validatorRules.deleted]" placeholder="请输入deleted"></a-input>
        </a-form-item>
        <a-form-item label="heat" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'heat', validatorRules.heat]" placeholder="请输入heat"></a-input>
        </a-form-item>
        <a-form-item label="score" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'score', validatorRules.score]" placeholder="请输入score"></a-input>
        </a-form-item>
        <a-form-item label="delivery_speed" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'deliverySpeed', validatorRules.deliverySpeed]" placeholder="请输入delivery_speed"></a-input>
        </a-form-item>
        <a-form-item label="role_img" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'roleImg', validatorRules.roleImg]" placeholder="请输入role_img"></a-input>
        </a-form-item>
        <a-form-item label="sort" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'sort', validatorRules.sort]" placeholder="请输入sort"></a-input>
        </a-form-item>
        <a-form-item label="remark" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'remark', validatorRules.remark]" placeholder="请输入remark"></a-input>
        </a-form-item>
        <a-form-item label="poster" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'poster', validatorRules.poster]" placeholder="请输入poster"></a-input>
        </a-form-item>
        <a-form-item label="content" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'content', validatorRules.content]" placeholder="请输入content"></a-input>
        </a-form-item>
        <a-form-item label="source" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'source', validatorRules.source]" placeholder="请输入source"></a-input>
        </a-form-item>
        <a-form-item label="tip" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'tip', validatorRules.tip]" placeholder="请输入tip"></a-input>
        </a-form-item>
        <a-form-item label="video" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'video', validatorRules.video]" placeholder="请输入video"></a-input>
        </a-form-item>
        <a-form-item label="property" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'property', validatorRules.property]" placeholder="请输入property"></a-input>
        </a-form-item>
        <a-form-item label="share_pic" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'sharePic', validatorRules.sharePic]" placeholder="请输入share_pic"></a-input>
        </a-form-item>
        <a-form-item label="buy_num" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'buyNum', validatorRules.buyNum]" placeholder="请输入buy_num"></a-input>
        </a-form-item>
        <a-form-item label="vip_price" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'vipPrice', validatorRules.vipPrice]" placeholder="请输入vip_price"></a-input>
        </a-form-item>
        
      </a-form>
    </a-spin>
    <a-button type="primary" @click="handleOk">确定</a-button>
    <a-button type="primary" @click="handleCancel">取消</a-button>
  </a-drawer>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  
  export default {
    name: "BlockShangpinModal",
    components: { 
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          name: {rules: [
          ]},
          firstName: {rules: [
          ]},
          salePrice: {rules: [
          ]},
          categoryJson: {rules: [
          ]},
          categoryIds: {rules: [
          ]},
          goodsLabelIds: {rules: [
          ]},
          logisticsModelId: {rules: [
          ]},
          galley: {rules: [
          ]},
          picUrl: {rules: [
          ]},
          detail: {rules: [
          ]},
          type: {rules: [
          ]},
          stock: {rules: [
          ]},
          isPers: {rules: [
          ]},
          isSell: {rules: [
          ]},
          browserNum: {rules: [
          ]},
          salesNum: {rules: [
          ]},
          studioId: {rules: [
          ]},
          nowDivide: {rules: [
          ]},
          divideInto: {rules: [
          ]},
          issueDate: {rules: [
          ]},
          createTime: {rules: [
          ]},
          updateTime: {rules: [
          ]},
          status: {rules: [
          ]},
          deleted: {rules: [
          ]},
          heat: {rules: [
          ]},
          score: {rules: [
          ]},
          deliverySpeed: {rules: [
          ]},
          roleImg: {rules: [
          ]},
          sort: {rules: [
          ]},
          remark: {rules: [
          ]},
          poster: {rules: [
          ]},
          content: {rules: [
          ]},
          source: {rules: [
          ]},
          tip: {rules: [
          ]},
          video: {rules: [
          ]},
          property: {rules: [
          ]},
          sharePic: {rules: [
          ]},
          buyNum: {rules: [
          ]},
          vipPrice: {rules: [
          ]},
        },
        url: {
          add: "/block/blockShangpin/add",
          edit: "/block/blockShangpin/edit",
        }
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'name','firstName','salePrice','categoryJson','categoryIds','goodsLabelIds','logisticsModelId','galley','picUrl','detail','type','stock','isPers','isSell','browserNum','salesNum','studioId','nowDivide','divideInto','issueDate','createTime','updateTime','status','deleted','heat','score','deliverySpeed','roleImg','sort','remark','poster','content','source','tip','video','property','sharePic','buyNum','vipPrice'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
         
        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'name','firstName','salePrice','categoryJson','categoryIds','goodsLabelIds','logisticsModelId','galley','picUrl','detail','type','stock','isPers','isSell','browserNum','salesNum','studioId','nowDivide','divideInto','issueDate','createTime','updateTime','status','deleted','heat','score','deliverySpeed','roleImg','sort','remark','poster','content','source','tip','video','property','sharePic','buyNum','vipPrice'))
      }
      
    }
  }
</script>

<style lang="less" scoped>
/** Button按钮间距 */
  .ant-btn {
    margin-left: 30px;
    margin-bottom: 30px;
    float: right;
  }
</style>